/**
 * 事件处理
 */
import {isArray, isFunction} from "star-map/shared/utils";

function event() {
    if (!this.event_list) {
        this.event_list = {};
    }
    this.MaxEventListNum = this.MaxEventListNum || undefined;
    this.defaultMaxEventLisNum = 10;
}

/**
 * 监听方法
 * @param {String} eventName 事件名
 * @param {function} callback 回调函数
 */
event.prototype.on = function (eventName, callback) {
    let _events, ctx;
    if (!_events) {
        _events = this.event_list = {}
    } else {
        ctx = this.event_list[eventName];
    }

    if (!ctx) {
        ctx = this.event_list[eventName] = callback;
        ctx.listenerCount = 1;
    } else if (isFunction(ctx)) {
        ctx = this.event_list[eventName] = [ctx, callback];
        ctx.listenerCount = ctx.length;
    } else if (isArray(ctx)) {
        let len = ctx.length;
        if (len > (this.MaxEventListNum ? this.MaxEventListNum : this.defaultMaxEventLisNum)) {
            throw new Error('Event.MaxEventListNum || [ MaxEventListNum ] :The number ' +
                'of subscriptions exceeds the maximum, and if you do not set it, the default value is 10');
        }
        ctx.push(callback);
        ctx.listenerCount = ctx.length;
    }
};
/**
 *
 * @param {String} eventName 事件名称
 * @param rest  其余参数
 * @returns {boolean}
 */
event.prototype.emit = function (eventName, ...rest) {
    let _event, ctx;
    _event = this.event_list;
    if (_event) {
        ctx = this.event_list[eventName];
    }
    if (!ctx) {
        return false
    } else if (isFunction(ctx)) {
        ctx.apply(this, rest);
    } else if (isArray(ctx)) {
        ctx.forEach((func) => {
            func.apply(this, rest);
        })
    }
};

// event.prototype.once = function (eventName,callback){
//     this.on(eventName, dealOnce(this, eventName, callback))
// };
/**
 * 移除指定事件的监听
 * @param eventName
 * @param callback
 */
event.prototype.removeListener = function (eventName, callback) {
    let ctx;
    ctx = this.event_list[eventName];
    if (callback) {
        if (isFunction(ctx)) {
            if (ctx === callback) {
                delete this.event_list[eventName];
            }
        } else if (isArray(ctx)) {
            this.event_list[eventName] = ctx.filter((item) => {
                return item !== callback;
            });
        }
    } else {
        if (ctx) {
            delete this.event_list[eventName];
        }
    }
};
/**
 * 移除所有监听
 * @returns {event}
 */
event.prototype.removeAllListeners = function (...rest) {
    let _events, ctx;
    _events = this.event_list;
    if (!_events) {
        return this;
    }
    if (rest.length === 0) {
        let keys = Object.keys(this.event_list);
        keys.forEach((key) => {
            delete this.event_list[key];
        });
    } else {
        rest.forEach((eventName) => {
            ctx = this.event_list[eventName];
            if (ctx) {
                delete this.event_list[eventName];
            }
        });
    }
};
// event.prototype.getListenerCount = function (){};

/**
 *
 * @param {event} target 事件管理
 * @param {String} eventName 事件名称
 * @param {function} callback 回调函数
 * @returns {undefined}
 */
// function dealOnce(target, eventName, callback) {
//     let flag = false;
//     function packageFunc(){
//         this.removeListener(eventName, packageFunc);
//         if(!flag){
//             flag = true;
//             callback.apply(target, arguments);
//         }
//     }
// }

let Event = new event();

export default Event;
